package site.yunnong.atvris.portal.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import site.yunnong.atvris.common.api.ResultCodeEnum;
import site.yunnong.atvris.common.exception.YunnongBusinessException;
import site.yunnong.atvris.portal.service.EsService;
import site.yunnong.atvris.portal.service.PartitionService;
import site.yunnong.atvris.service.MediaPartitionService;
import site.yunnong.atvris.common.enumeration.PartitionLevelEnum;
import site.yunnong.atvris.common.enumeration.VideoSortEnum;
import site.yunnong.atvris.common.enumeration.VideoTimeTypeEnum;
import site.yunnong.atvris.common.dto.SearchUserParam;
import site.yunnong.atvris.common.dto.SearchVideoParam;
import site.yunnong.atvris.service.vo.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zjh
 * @date 2021/8/20 12:00
 */
@Slf4j
@Service
public class EsServiceImpl implements EsService {
    
    @Resource
    private MediaPartitionService mediaPartitionService;
    
    @Resource
    private PartitionService partitionService;
    
    @Override
    public List<SearchStaticSortVO> getSearchStaticSortEnum() {
        return Arrays.stream(VideoSortEnum.values()).map(this::buildSearchStaticSortVO).collect(Collectors.toList());
    }

    @Override
    public List<VideoTimeTypeVO> getSearchVideoTimeTypeEnum() {
        return Arrays.stream(VideoTimeTypeEnum.values()).map(this::buildVideoTimeTypeVO).collect(Collectors.toList());
    }

    @Override
    public SearchVideoConditionVO getSearchVideoCondition() {
        SearchVideoConditionVO vo = new SearchVideoConditionVO();
        // 搜索视频排序
        vo.setSearchStaticSortVOList(getSearchStaticSortEnum());
        // 搜索视频时长
        vo.setVideoTimeTypeVOList(getSearchVideoTimeTypeEnum());
        List<PartitionVO> partitionVOList = partitionService.queryAll();
        if (CollectionUtils.isEmpty(partitionVOList)) {
            throw new YunnongBusinessException(ResultCodeEnum.ERROR_BUSINESS, "分区数据库异常, 未获取到分区数据库中的数据");
        }
        Map<Integer, List<PartitionVO>> collect = partitionVOList.stream().collect(Collectors.groupingBy(PartitionVO::getPartitionLevel));
        // 一级分区
        vo.setFirstLevelPartitionList(collect.get(PartitionLevelEnum.FIRST_LEVEL.getValue()));
        vo.setSecondLevelPartitionList(collect.get(PartitionLevelEnum.SECOND_LEVEL.getValue()));
        vo.setThirdLevelPartitionList(collect.get(PartitionLevelEnum.THIRD_LEVEL.getValue()));
        return vo;
    }

    private VideoTimeTypeVO buildVideoTimeTypeVO(VideoTimeTypeEnum videoTimeTypeEnum) {
        return VideoTimeTypeVO.builder().value(videoTimeTypeEnum.getValue())
                .desc(videoTimeTypeEnum.getDesc())
                .build();
    }

    private SearchStaticSortVO buildSearchStaticSortVO(VideoSortEnum videoSortEnum) {
        return SearchStaticSortVO.builder()
                .value(videoSortEnum.getValue())
                .desc(videoSortEnum.getDesc())
                .build();
    }
}
